package com.thd.springboot.project.camundamanager.utils;

import com.thd.springboot.project.camundamanager.entity.*;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;

/**
 * camunda entity 转换成通用流程实体工具类
 */
public class CamundaEntityTransferUtils {
    public static ProcDefEntity transfer(ProcessDefinition processDefinition){
        ProcDefEntity entity = ProcDefEntity.createInstance()
                .setProcDefId(processDefinition.getId())
                .setDefKey(processDefinition.getKey())
                .setDefName(processDefinition.getName())
                .setDefVersion(String.valueOf(processDefinition.getVersion()))
                .setCategory(processDefinition.getCategory());
        return entity;
    };

    public static ProcDeployEntity transfer(Deployment deployment){
        ProcDeployEntity entity = ProcDeployEntity.createInstance()
                .setDeploymentId(deployment.getId())
                .setDeployTime(deployment.getDeploymentTime());
        return entity;
    }

    public static ProcessInstanceEntity transfer(ProcessInstance processInstance){
        ProcessInstanceEntity entity = ProcessInstanceEntity.createInstance()
                .setBusinessKey(processInstance.getBusinessKey())
                .setProcessInstanceId(processInstance.getProcessInstanceId());
        return entity;
    }

    public static ProcessInstanceEntity transfer(HistoricProcessInstance processInstanceHis){
        ProcessInstanceEntity entity = ProcessInstanceEntity.createInstance()
                .setBusinessKey(processInstanceHis.getBusinessKey())
                .setProcessInstanceId(processInstanceHis.getId());

        entity.setProcessInstanceStartTime(processInstanceHis.getStartTime());
        entity.setProcessInstanceEndTime(processInstanceHis.getEndTime());
        return entity;
    }

    public static ProcessInstanceTaskEntity transfer(Task task){
        ProcessInstanceTaskEntity entity = ProcessInstanceTaskEntity.createInstance()
                .setExecutionId(task.getExecutionId())
                .setTaskAssignee(task.getAssignee())
                .setTaskId(task.getId())
                .setTaskDefKey(task.getTaskDefinitionKey())
//                .setTaskFormKey(task.getFormKey())
                .setTaskOwner(task.getOwner())
                .setTaskStartTime(task.getCreateTime());

        entity.setProcessInstanceId(task.getProcessInstanceId());
        entity.setExecutionId(task.getExecutionId());
        return entity;
    }
    public static ProcessInstanceTaskEntity transfer(HistoricTaskInstance taskHis){
        ProcessInstanceTaskEntity entity  = ProcessInstanceTaskEntity.createInstance()
                .setTaskEndTime(taskHis.getEndTime())
                .setTaskStartTime(taskHis.getStartTime());
        entity.setTaskId(taskHis.getId());
        entity.setTaskAssignee(taskHis.getAssignee());
        entity.setTaskDefKey(taskHis.getTaskDefinitionKey());
        entity.setTaskOwner(taskHis.getOwner());
        entity.setProcessInstanceId(taskHis.getProcessInstanceId());
        entity.setExecutionId(taskHis.getExecutionId());
        entity.setTaskName(taskHis.getName());
        return entity;
    }

}
